package cn.hfjava.kz.service.impl;

import cn.hfjava.kz.entity.dto.ExamAdmissionQueryDTO;
import cn.hfjava.kz.entity.excel.ExamAdmissionExcel;
import cn.hfjava.kz.entity.vo.PageVO;
import cn.hfjava.kz.exception.BizException;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hfjava.kz.entity.po.ExamAdmissionTicket;
import cn.hfjava.kz.service.ExamAdmissionTicketService;
import cn.hfjava.kz.mapper.ExamAdmissionTicketMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author 29264
* @description 针对表【exam_admission_ticket】的数据库操作Service实现
* @createDate 2024-08-06 09:05:57
*/
@Service
public class ExamAdmissionTicketServiceImpl extends ServiceImpl<ExamAdmissionTicketMapper, ExamAdmissionTicket>
    implements ExamAdmissionTicketService{
    @Resource
    private  ExamAdmissionTicketMapper examAdmissionTicketMapper;


    @Override
    public PageVO<ExamAdmissionTicket> findByPage(ExamAdmissionQueryDTO dto) {
        if (Objects.isNull(dto)) {
            throw new BizException(500, "查询条件对象为null");
        }

        // 过滤空字符串，将它们设置为null
        if (StringUtils.isEmpty(dto.getName())) {
            dto.setName(null);
        }
        if (StringUtils.isEmpty(dto.getIdNum())) {
            dto.setIdNum(null);
        }
        if(StringUtils.isEmpty(dto.getExamNum())){
            dto.setExamNum(null);
        }
        if(StringUtils.isEmpty(dto.getProfession())){
            dto.setProfession(null);
        }
        if(StringUtils.isEmpty(dto.getExamType())){
            dto.setExamType(null);
        }
        if(StringUtils.isEmpty(dto.getMakeupType())){
            dto.setMakeupType(null);
        }
        if(StringUtils.isEmpty(dto.getCompanyName())){
            dto.setCompanyName(null);
        }
        if(StringUtils.isEmpty(dto.getBatchName())){
            dto.setBatchName(null);
        }
        if(StringUtils.isEmpty(dto.getStartTime())){
            dto.setStartTime(null);
        }
        if(StringUtils.isEmpty(dto.getEndTime())){
            dto.setEndTime(null);
        }
        LambdaQueryWrapper<ExamAdmissionTicket> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(dto.getName()), ExamAdmissionTicket::getName, dto.getName());
        wrapper.like(StringUtils.hasLength(dto.getIdNum()), ExamAdmissionTicket::getIdNum, dto.getIdNum());
        wrapper.like(StringUtils.hasLength(dto.getExamNum()), ExamAdmissionTicket::getExamNum, dto.getExamNum());
        wrapper.like(StringUtils.hasLength(dto.getProfession()), ExamAdmissionTicket::getProfession, dto.getProfession());
        wrapper.like(StringUtils.hasLength(dto.getExamType()), ExamAdmissionTicket::getExamType, dto.getExamType());
        wrapper.like(StringUtils.hasLength(dto.getMakeupType()), ExamAdmissionTicket::getMakeupType, dto.getMakeupType());
        wrapper.like(StringUtils.hasLength(dto.getCompanyName()), ExamAdmissionTicket::getCompanyName, dto.getCompanyName());
        wrapper.like(StringUtils.hasLength(dto.getBatchName()), ExamAdmissionTicket::getBatchName, dto.getBatchName());
        wrapper.like(StringUtils.hasLength(dto.getStartTime()), ExamAdmissionTicket::getStartTime, dto.getStartTime());
        wrapper.like(StringUtils.hasLength(dto.getEndTime()), ExamAdmissionTicket::getEndTime, dto.getEndTime());

        IPage<ExamAdmissionTicket> page = new Page<>(dto.getCurPage(), dto.getPageSize());

        wrapper.select(ExamAdmissionTicket.class, info -> !info.getColumn().equals("delected"));
        IPage<ExamAdmissionTicket> result = examAdmissionTicketMapper.selectPage(page, wrapper);
        PageVO<ExamAdmissionTicket> pageVO = new PageVO<>();
        pageVO.setCurrentPage(result.getCurrent());
        pageVO.setPageSize(result.getPages());
        pageVO.setTotal(result.getTotal());
        pageVO.setList(result.getRecords());
        return pageVO;
    }

    @Override
    public List<ExamAdmissionExcel> selectAllExam() {
        return examAdmissionTicketMapper.selectAllExam();
    }

    @Override
    public List<ExamAdmissionExcel> findStudentExcelByIdList(List<Integer> idList) {
        List<ExamAdmissionTicket> examAdmissionTickets = examAdmissionTicketMapper.selectBatchIds(idList);
        ArrayList<ExamAdmissionExcel> examAdmissionExcels = new ArrayList<>();
        examAdmissionTickets.forEach(examAdmissionTicket -> {
            ExamAdmissionExcel examAdmissionExcel = new ExamAdmissionExcel();
            BeanUtils.copyProperties(examAdmissionTicket,examAdmissionExcel);
            examAdmissionExcels.add(examAdmissionExcel);

        });
        return examAdmissionExcels;
    }

    @Override
    public void saveExam(List<ExamAdmissionExcel> examAdmissionExcelList) {
        ArrayList<ExamAdmissionTicket> examAdmissionTickets = new ArrayList<>();
        examAdmissionExcelList.forEach(examAdmissionExcel -> {
            ExamAdmissionTicket examAdmissionTicket = new ExamAdmissionTicket();
//            examAdmissionTicket.setName(examAdmissionExcel.getName());
//            examAdmissionTicket.setIdNum(examAdmissionExcel.getIdNum());
//            examAdmissionTicket.setExamNum(examAdmissionExcel.getExamNum());
//            examAdmissionTicket.setSeatNum(examAdmissionExcel.getSeatNum());
//            examAdmissionTicket.setSeatNumber(examAdmissionExcel.getSeatNumber());
//            examAdmissionTicket.setSession(examAdmissionExcel.getSession());
            BeanUtils.copyProperties(examAdmissionExcel,examAdmissionTicket);
            examAdmissionTicket.setCreateTime(new Date());
            examAdmissionTicket.setDeleted(0);
            System.out.println(examAdmissionTicket.getStartTime());
            System.out.println(examAdmissionTicket.getStartTime());
            examAdmissionTickets.add(examAdmissionTicket);

        });
        examAdmissionTicketMapper.saveExam(examAdmissionTickets);
    }

    @Override
    public List<ExamAdmissionTicket> getAllExam() {
        return examAdmissionTicketMapper.getAllExam();
    }
}




